from manim import *
class CustomCurve(VMobject):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 定义自定义曲线的控制点
        points = [
            [15, 20, 0],
            [14,15,0],
            [10, 12.5, 0],
            [14,10,0],
            [15, 5, 0],
            
        ]
        self.set_points_smoothly(points)

class CustomCurve1(VMobject):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 定义自定义曲线的控制点
        points = [
            [25, -15, 0],
            [20,-20,0],
            [25,-25,0],
        ]
        self.set_points_smoothly(points)



class math1(MovingCameraScene):
    def construct(self):
        self.camera.background_color = BLACK  # 设置背景颜色
        self.camera.frame_width = 100  # 设置边框宽度
        self.camera.frame_height = 60  # 设置边框高度
        self.camera.pixel_height = 1080  # 设置像素高度
        self.camera.pixel_width = 1920  # 设置像素宽度
        self.camera.center = ORIGIN  # 设置中心点位置
        self.camera.scale_factor = 1.0  # 设置缩放因子

        #设置横线
        for i in range(6*2+1):       
            dot1=Dot([-50,5*(i-6),0]).set_opacity(0.5)
            dot2=Dot([50,5*(i-6),0]).set_opacity(0.5)
            if i==6:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
                
            else:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
               
            self.add(dot1,dot2,line1)
        #设置竖线
        for i in range(10*2+1):        
            dot3=Dot([(i-10)*5,-30,0]).set_opacity(0.5)
            dot4=Dot([(i-10)*5,30,0]).set_opacity(0.5)
            if i==10:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
                
            else:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
            self.add(dot3,dot4,line2)
        #设置三个点
        dot1 = Dot(radius=1, color=RED)  
        dot1.move_to([-47.5,27.5,0]) 
        dot2 = Dot(radius=1, color=YELLOW)  
        dot2.move_to([-42.5,27.5,0])  
        dot3 = Dot(radius=1, color=GREEN)  
        dot3.move_to([-37.5,27.5,0])
        #镜头跟进效果 
        self.camera.frame.save_state()
        #题目出现
        
        text = Tex(r"$\int _{0}^{\pi }\dfrac{x}{2+\sin x}dx$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,20,0])
        text3 = Tex(r"\text{芳900例1:}",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text,LEFT,buff=1)
        text1 = Tex(
                   r"$f\left( x\right) =\dfrac{1}{2+\sqrt{1-x^{2}}}+\dfrac{1}{1+\cos \dfrac{\pi }{2}x+2\sin \dfrac{\pi }{2}x}\int _{0}^{1}f\left( x\right) dx$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([5,5,0])
        text4 = Tex(
                   r"\text{芳900例2:}",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-35,6,0])
        text2 = Tex(r"\text{求}$f\left( x\right)$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-23,-2,0])
        self.play(Write(text),self.camera.frame.animate.scale(0.9),Write(text1),Write(text2),Write(text3),Write(text4))
        
        #镜头拉回
          #分析文本
        #text100=MarkupText("<b><i>分析/解：</i></b>",color=PINK).scale(5.0).move_to([-38,12.5,0])
        self.play(Restore(self.camera.frame))
        self.play(FadeIn(dot1),run_time=0.1)
        self.play(FadeIn(dot2),run_time=0.1)
        self.play(FadeIn(dot3),run_time=0.1)
        
        text5 = Tex(
                   r"$\int \dfrac{1}{k+m\sin x+n\cos x}dx$",color=WHITE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,0,0])
        self.play(
            ReplacementTransform(text,text5),
            ReplacementTransform(text1,text5),
            ReplacementTransform(text2,text5),
            ReplacementTransform(text3,text5),
            ReplacementTransform(text4,text5)
        )
        self.wait(0.5)
         #箭头引出
        arrow = Arrow(start=[10,10,0],end=[5,5,0], color=RED
                       , stroke_width=20, tip_length=1)
        
        #文本
        long_text=("分子为常数1")
        text6 = Text(long_text,color=YELLOW,
                   font_size=200).move_to([20,10,0])
        self.play(Create(arrow))
        self.play(Create(text6))
        self.wait(0.2)
        #箭头引出
        arrow1 = Arrow(start=[10,-10,0],end=[5,-5,0], color=RED
                       , stroke_width=20, tip_length=1)
        text7 = Tex(
                   r"\text{分母为}$\sin x,\cos x$\text{一次}",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([30,-10,0])
        self.play(Create(arrow1))
        self.play(Create(text7))
        self.wait(0.2)
        text8 = Tex(
                   r"\text{特征碎片：万能公式}",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,22,0])
        self.play(
            ReplacementTransform(arrow,text8),
            ReplacementTransform(arrow1,text8),
            ReplacementTransform(text7,text8),
            ReplacementTransform(text6,text8)
            
        )
        self.wait(0.5)
        text9 = Tex(
                   r"\text{令}$t=\tan \dfrac{x}{2}$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-15,12,0])
        self.play(Write(text9))
        #箭头
        arrow2 = Arrow(start=[-7,12,0],end=[7,12,0],color=RED
                       , stroke_width=20, tip_length=1)

        self.play(GrowArrow(arrow2))
        curve = CustomCurve(color=PURPLE, stroke_width=20)
        
        # 添加到场景并播放动画
        self.play(Create(curve))
        self.wait()
        text10 = Tex(
                   r"$\sin x=\dfrac{2t}{1+t^{2}}$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([25,20,0])
        text11 = Tex(
                   r"$\cos x=\dfrac{1-t^{2}}{1+t^{2}}$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([25,12.5,0])
        text12 = Tex(
                   r"$dx=\dfrac{2}{1+t^{2}}$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([25,5,0])
        self.play(Write(text11),Write(text10),Write(text12))

        text13 = Tex(
                   r"$\int \dfrac{1}{k+m\dfrac{2t}{1+t^{2}}+n\dfrac{1-t^{2}}{1+t^{2}}}\cdot \dfrac{2}{1+t^{2}}dt$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-10,0])
        text14 = Tex(
                   r"$\int \dfrac{2}{\left( k-n\right) t^{2}+2mt+k+n}dt$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-10,0])
        self.play(Write(text13))
        
        self.wait(0.5)
        self.play(FadeOut(text5))
        self.play(
            ReplacementTransform(text13,text14)
        )
        self.wait(0.5)
        text15 = Tex(
                   r"\text{(有理函数)}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text14,RIGHT,buff=2)
        self.play(Write(text15))
        #箭头
        #箭头引出
        arrow3 = Arrow(start=[-10,-20,0],end=[-5,-15,0], color=RED
                       , stroke_width=20, tip_length=1)
        
        #文本
        long_text=("分母为二次函数")
        text16 = Text(long_text,color=YELLOW,
                   font_size=200).move_to([-20,-20,0])
        self.play(Create(arrow3))
        self.play(Create(text16))
        self.wait(0.2)
        #箭头
        arrow4 = Arrow(start=[-9,-20,0],end=[-1,-20,0],color=RED
                       , stroke_width=20, tip_length=1)

        self.play(GrowArrow(arrow4))

        text17 = Tex(
                   r"\text{配方}",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([5,-20,0])
        self.play(Write(text17))
        #箭头
        arrow5 = Arrow(start=[9,-20,0],end=[19,-20,0],color=RED
                       , stroke_width=20, tip_length=1)

        self.play(GrowArrow(arrow5))
        curve1 = CustomCurve1(color=PURPLE, stroke_width=20)
        
        # 添加到场景并播放动画
        self.play(Create(curve1))
        self.wait()
        text18 = Tex(
                   r"$\int \dfrac{1}{a^{2}+t^{2}}dt$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([35,-15,0])
        
        text19 = Tex(
                   r"$\int \dfrac{1}{a^{2}-t^{2}}dt$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([35,-25,0])
        self.play(Write(text19),Write(text18))
        #框框标记
        rectangle=Rectangle(color=BLUE,fill_opacity=0.0,width=15,height=15
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle.move_to([35,-20,0])
        self.play(Create(rectangle))
        self.wait()
      